//
//  HMEmoticonKeyboardView.swift
//  WB25
//
//  Created by HM on 2017/7/10.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 刷新指定组和页(刷新最近表情页)
    func reloadRecentEmoticons(){
        let indexPath = IndexPath(item: 0, section: 0)
        pageView.reloadItems(at: [indexPath])
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        // 设置大小
        self.frame.size = CGSize(width: HMSCREENW, height: 216)
        backgroundColor = UIColor(patternImage: UIImage(named:"emoticon_keyboard_background")!)
        
        // 添加控件
        addSubview(bottomView)
        addSubview(pageView)
        addSubview(pageControl)
        // 添加约束
        bottomView.snp.makeConstraints { (make) in
            make.height.equalTo(35)
            make.left.bottom.right.equalTo(self)
        }
        
        pageView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(pageView).offset(5)
        }
        
        
        // 监听底部视图的按钮点击
        bottomView.closure = {[weak self](type) in
            switch type {
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("emoji")
            case .lxh:
                print("浪小花")
            }
            
            // 滚动到指定的组
            let indexPath = IndexPath(item: 0, section: type.rawValue)
            self?.pageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            
            // 设置分页指示器切换
            self?.setupPageControl(indexPath: indexPath)
        }
        
    }
    
    //MARK: - 懒加载控件
    fileprivate lazy var bottomView: HMEmoticonBottomView = HMEmoticonBottomView()
    // 自定义表情键盘配图
    fileprivate lazy var pageView: HMEmoticonPageView = {
        let view = HMEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        view.delegate = self
        // 分页
        view.isPagingEnabled = true
        // 弹簧效果
        view.bounces = false
        // 隐藏滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
    }()
    
    // 分页指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let view = UIPageControl()
        // 设置总页数
        view.numberOfPages = HMEmoticonTools.share.allEmoticons[1].count
        // 当前页数
        view.currentPage = 0
        // 选中的颜色
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named:"compose_keyboard_dot_selected")!)
//        // 其他的颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named:"compose_keyboard_dot_normal")!)
        /*
            在xcode 7 的时候有两个只读计算型属性 pageImage  currentPageImage
            - 但是可以使用KVC进行赋值
         */
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
        view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        // 取消用户相应
        view.isUserInteractionEnabled = false
        // 当numberOfPages==1 默认隐藏
        view.hidesForSinglePage = true
        return view
    }()

}
//MARK: -UICollectionViewDelegate
extension HMEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 获取中心x点
        let centerX = HMSCREENW * 0.5 + scrollView.contentOffset.x
        // 判断那个cell显示的多(当前表情配图上应该有2个cell) 那么就通过cell 获取对应indexPath -> section
        // 当前屏幕展示的cell个个数
        let visibleCells = pageView.visibleCells.sorted { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        if visibleCells.count == 2 {
            // 分别获取第一个cell 和最后已给cell
            let firstCell = visibleCells.first!
            let lastCell = visibleCells.last!
            let indexPath: IndexPath
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
//                print("第一个cell显示的多")
                indexPath = pageView.indexPath(for: firstCell)!
            }else{
//                print("第二个cell显示的多")
                indexPath = pageView.indexPath(for: lastCell)!
            }
            
            // 得到最终的indexPath
            bottomView.setupSelectButton(tag: indexPath.section)
            setupPageControl(indexPath: indexPath)
        }
        
        
    }
    // 设置pageControl的公共方法
    fileprivate func setupPageControl(indexPath: IndexPath){
        pageControl.numberOfPages =  HMEmoticonTools.share.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
}








